package com.service.schedule.tm;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.DateUtils;
import com.common.http.HttpResult;
import com.dao.tm.PlanDao;
import com.enums.FactoryEnum;
import com.model.tm.PlanModel;
import com.service.external.oncard.EmsService;

@Service("EmsScheduleService")
public class EmsScheduleService {
	static final Logger logger = LoggerFactory.getLogger(EmsScheduleService.class);

	@Autowired
	PlanDao planDao;
	@Autowired
	EmsService emsService;

	// 获取运单号
	public void getEms() {
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("emsBillNumExt", "1");
		hashMap.put("startTime", DateUtils.addDays(DateUtils.getCurrentDate(), -10));
		List<PlanModel> planModels = planDao.selectList(hashMap);
		String godNumXy = planModels.stream().filter(s -> s.getFacCode().equals(FactoryEnum.xy.getValue()))
				.map(PlanModel::getGodNum).collect(Collectors.joining("','", "'", "'"));
		String godNumSp = planModels.stream().filter(s -> s.getFacCode().equals(FactoryEnum.sp.getValue()))
				.map(PlanModel::getGodNum).collect(Collectors.joining("','", "'", "'"));
		String godNumXyHb = planModels.stream().filter(s -> s.getFacCode().equals(FactoryEnum.hb.getValue()))
				.map(PlanModel::getGodNum).collect(Collectors.joining("','", "'", "'"));

		HttpResult httpResult = emsService.getWeight(godNumXy, FactoryEnum.xy.getValue());
		List<Object[]> arrayListResult = (List<Object[]>) httpResult.getData();
		updateByCorp(arrayListResult);

		httpResult = emsService.getWeight(godNumSp, FactoryEnum.sp.getValue());
		arrayListResult = (List<Object[]>) httpResult.getData();
		updateByCorp(arrayListResult);

//		httpResult = emsService.getWeight(godNumXyHb, FactoryEnum.hb.getValue());
//		arrayListResult = (List<Object[]>) httpResult.getData();
//		updateByCorp(arrayListResult);

		httpResult = emsService.getWeight(godNumXyHb, FactoryEnum.hk.getValue());
		arrayListResult = (List<Object[]>) httpResult.getData();
		updateByCorp(arrayListResult);
	}

	public void updateByCorp(List<Object[]> arrayListResult) {
		for (Object[] arr : arrayListResult) {
			PlanModel planModel = new PlanModel();
			planModel.setEmsBillNum(arr[0].toString());
			planModel.setEmsInTime(arr[1] != null ? arr[1].toString() : null);
			planModel.setEmsOutTime(arr[2] != null ? arr[2].toString() : null);
			planModel.setEmsGross(arr[3] != null ? new BigDecimal(arr[3].toString()) : null);
			planModel.setEmsTare(arr[4] != null ? new BigDecimal(arr[4].toString()) : null);
			planModel.setEmsNnum(arr[5] != null ? new BigDecimal(arr[5].toString()) : null);
			planModel.setGodNum(arr[6].toString());
			planDao.update(planModel);
		}
	}
}
